(function () {
    window.Game = class Game {
        constructor (options) {
            // 帧数
            this.fps = options.fps
            // 画布
            this.canvas = document.getElementById(options.id)
            this.canvas.width = document.documentElement.clientWidth
            this.canvas.height = document.documentElement.clientHeight
            this.context = this.canvas.getContext('2d')
            // 帧工具
            this.frameUtil = new FrameUtil(options.fps)
            // 游戏状态
            this.state = 0
            // 静态资源
            this.images = null
            this.StaticResouces = new StaticResouces()
            this.StaticResouces.loadImages('imagesList.js',(already,total, images) => {
                this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
                this.context.fillText(already  + 'of' + total,30,30)
                if (already === total) {
                    this.images = images
                    this.run()
                }
            }) 
        }
        run () {
            this.racket = new Racket()
            this.ball = new Ball()
            this.life = new Life()
            this.blockManger = new BlockManager()
            this.blockManger.createMap()
            this.blockManger.render()
            setInterval(() => { 
                this.mainloop()
            },1000 / this.fps)
        }
        // 主循环业务
        mainloop () {
            this.context.clearRect(0, 0, this.canvas.width, this.canvas.height)
            this.blockManger.render()
            this.racket.render()
            this.ball.update()
            this.ball.render()
            this.life.render()
            this.frameUtil.update()
            this.frameUtil.render()
        }
        // 游戏结束
        over () {
            over.style.display = 'block'
        }
        // 游戏重置
        reset () {
            this.ball.reset()
            this.state = 0
            this.blockManger.createMap()
            this.blockManger.render()
        }
    }
})()